package net.onceface.web.resources;

import javax.inject.Inject;
import javax.ws.rs.BeanParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import net.onceface.application.AddressApplication;
import net.onceface.application.commons.InvokeResult;
import net.onceface.application.commons.Message;
import net.onceface.application.dto.AddressDTO;

/**
 * 用户收货地址资源类
 * **/
@Path("/address")
public class AddressResources extends BaseResource{

	@Inject
	private AddressApplication application;
	
	/**
	 * 获取用户默认收货地址
	 * **/
	@GET
	@Path("/getAddress")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public String getAddress(@QueryParam("clientId")final Long clientId){
		try{
			return toJson(InvokeResult.success(application.getByClientId(clientId)));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.GET_ADDRESS_FAILURE));
		}
	}
	
	/**
	 * 获取用户收货地址列表
	 * **/
	@GET
	@Path("/getList")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public String getList(@QueryParam("clientId")final Long clientId){
		try{
			return toJson(InvokeResult.success(application.findByClientId(clientId)));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.GET_ADDRESS_FAILURE));
		}
	}
	
	/***
	 * 修改收货地址
	 * **/
	@POST
	@Path("/edit")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public InvokeResult edit(@BeanParam AddressDTO address){
		try{
			return application.edit(address);
		}catch(Exception e){
			return InvokeResult.failure(Message.EDIT_ADDRESS_FAILURE);
		}
	}
	
	/**
	 * 设置默认收货地址
	 * **/
	@POST
	@Path("/setDef")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public InvokeResult setDef(@FormParam("id")final Long id, 
						@FormParam("clientId")final Long clientId){
		try{
			return application.setDef(id, clientId);
		}catch(Exception e){
			return InvokeResult.failure(Message.EDIT_ADDRESS_FAILURE);
		}
	}
	
	/**
	 * 添加收货地址
	 * **/
	@POST
	@Path("/add")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public InvokeResult add(@BeanParam AddressDTO address){
		try{
			return application.edit(address);
		}catch(Exception e){
			return InvokeResult.failure(Message.ADD_ADDRESS_FAILURE);
		}
	}
	
	/**
	 * 删除收货地址
	 * **/
	@POST
	@Path("/remove")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public InvokeResult remove(@FormParam("id")final Long id){
		try{
			return application.remove(id);
		}catch(Exception e){
			return InvokeResult.failure(Message.REMOVE_ADDRESS_FAILURE);
		}
	}
}
